# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=miniupnpc
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=2.2.3
pkgrel=2
pkgdesc="A small UPnP client library/tool to access Internet Gateway Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://miniupnp.free.fr"
license=('BSD')
options=('staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
source=(https://miniupnp.tuxfamily.org/files/${_realname}-${pkgver}.tar.gz{,.sig}
        https://github.com/miniupnp/miniupnp/commit/f82b056.patch
        001-fix-static-library-name.patch
        002-python-module-build.patch)
sha256sums=('dce41b4a4f08521c53a0ab163ad2007d18b5e1aa173ea5803bd47a1be3159c24'
            'SKIP'
            '89f4069957931474fc6f0d9ead825c1d4a7a150d9a60833435114c6fa45d4f03'
            'b087e2f494ee24853a1d67a38d7eb770da2481e1124d60dcb40c0f5734f6a14c'
            '79857b7808ece306cfae0bfb6c072a0022aaba1d8624273d51ed4e4fc741509c')
validpgpkeys=('751E9FF6944A3B36A5432216DB511043A31ACAAF') # miniupnp <miniupnp@free.fr>

prepare() {
  cd ${_realname}-${pkgver}
  patch -p2 -i ${srcdir}/f82b056.patch

  # https://github.com/miniupnp/miniupnp/pull/604
  patch -p1 -i ${srcdir}/001-fix-static-library-name.patch
  patch -p1 -i ${srcdir}/002-python-module-build.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUPNPC_BUILD_SHARED=ON \
    -DUPNPC_BUILD_STATIC=ON \
    .

  ${MINGW_PREFIX}/bin/cmake --build .

  msg "Build Python extension..."
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py build
}

package() {
  cd build-${MSYSTEM}
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/man3/miniupnpc.3 \
    "${pkgdir}${MINGW_PREFIX}/share/man/man3/miniupnpc.3"
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  msg "Install Python${pyt} extension..."
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"
}
